package per.turtle.farm.controller;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import per.turtle.common.websocket.WebSocketServer;
import per.turtle.farm.po.User;
import per.turtle.farm.service.LoginService;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.io.IOException;
import java.util.Arrays;

/**
 * @author admin
 */
@Controller
public class Login {

    @Autowired
    LoginService loginservice;

    @Autowired
    private WebSocketServer socketServer;

    @RequestMapping(value = "/currentuser", method = RequestMethod.GET)
    @ResponseBody
    public User currentuser(HttpSession httpSession) {
        Long uid = (Long) httpSession.getAttribute("uid");
        String name = loginservice.getnamebyid(uid);
        return new User(uid, name);
    }

    @RequestMapping(value = "/currentuser1", method = RequestMethod.GET)
    @ResponseBody
    public User currentuser1(HttpSession httpSession) {
        return new User(11111L, "yujia");
    }

    @GetMapping("/ewm/{user}")
    public void ewm(HttpServletResponse response,@PathVariable("user") String username) throws IOException {
        QrConfig config = new QrConfig(300, 300);
        // 设置边距，既二维码和背景之间的边距
        config.setMargin(3);
        // 设置前景色，既二维码颜色（青色）
        config.setForeColor(Color.CYAN);
        // 设置背景色（灰色）
        config.setBackColor(Color.GRAY);
        // 生成二维码到文件，也可以到流
        QrCodeUtil.generate("http://192.168.50.17:8080/farm/picup/"+username, config, ImgUtil.IMAGE_TYPE_PNG, response.getOutputStream());
    }

    @RequestMapping("/broom")
    public String broom() {
        return "broom";
    }

    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    @RequestMapping("/loginvalidate")
    public String loginvalidate(@RequestParam("username") String username, @RequestParam("password") String pwd, HttpSession httpSession) {
        if (username == null) {
            return "login";
        }
        String realpwd = loginservice.getpwdbyname(username);
        if (pwd.equals(realpwd)) {
            long uid = loginservice.getUidbyname(username);
            httpSession.setAttribute("uid", uid);
            return "chatroom";
        } else {
            return "fail";
        }
    }

    @RequestMapping("/picup/{user}")
    public String picup(@PathVariable("user") String username) {
        return "picup";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession httpSession) {
        return "login";
    }

    @GetMapping("/upTemp")
    public void upTemp(@RequestPart(value = "files") MultipartFile[] files) throws IOException {
        for (MultipartFile file : files) {
            socketServer.sendInfo("TOM", Arrays.toString(file.getBytes()));
        }
    }

}
